function findStr(str, pattern) {
  let state = start
  for (let i = 0; i < str.length; i++) {
    state = state(str[i])
    if (state === success) {
      return i - pattern.length + 1
    }
  }
  return -1
}

function start(c) {
  if (c === 'a') {
    return foundA
  } else {
    return start
  }
}

function foundA(c) {
  if (c === 'b') {
    return foundB
  } else {
    return start(c)
  }
}

function foundB(c) {
  if (c === 'c') {
    return foundC
  } else {
    return start(c)
  }
}

function foundC(c) {
  if (c === 'd') {
    return foundD
  } else {
    return start(c)
  }
}

function foundD(c) {
  if (c === 'e') {
    return foundE
  } else {
    return start(c)
  }
}

function foundE(c) {
  if (c === 'f') {
    return success
  } else {
    return start(c)
  }
}

function success(c) {
  return success
}



console.log(
  findStr('aabcdef', 'abcde')
)

module.exports = {
  findStr
}
